import {
  getUserInfo,
  createWechat
} from "../../../services/_utils/api"
import {
  GETSTORAGE,
  SETSTORAGE
} from "../../../services/storage/storage"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    familyMemberId: '',
    min: 3,
    timer: ''
  },
  getUserInfo(val) {
    const user = GETSTORAGE('userId')
    console.log(user, 'id345');
    getUserInfo({
      wechatMiniprogramOpenId: '',
      userId: user,
      wechatServiceOfficialAccountOpenId: '',
      wechatUnionId: ''
    }).then(res => {
      console.log(res, "当前用户信息");
      if (res.data.data == null || res.data.code == 500 || res.data.msg == '用户不存在') {
        // 新用户
        const openid = GETSTORAGE('openid')
        const unionid = GETSTORAGE('unionid')
        let obj = {
          miniprogramOpenid: openid, //小程序openid
          unionid: unionid,
          serviceOfficialAccountOpenid: ''
        }
        createWechat(obj).then(res => {
          console.log(res, '创建用户');
          SETSTORAGE('userId', res.data.data.userId)
          this.delayedBranch(val)
        })
      } else {
        // 老用户
        const {
          currentAgreementVersion,
          signedAgreementVersion
        } = res.data.data
        if (currentAgreementVersion == signedAgreementVersion) {
          this.enter(val)
        } else {
          this.delayedBranch(val)
        }
      }
    })
  },
  delayedBranch(val) {
    clearInterval(this.data.timer)
    const time = setInterval(() => {
      this.setData({
        min: this.data.min - 1,
        timer: time
      })
      console.log(this.data.min);
      if (this.data.min < 1 || val == 'immediately') {
        console.log(time, this.data.timer);
        clearInterval(time)
        const msg = 'invite'
        wx.navigateTo({
          url: `/pages/caidan1/caidanyi?invite=${msg}&familyMemberId=${this.data.familyMemberId}`,
        })
        console.log('跳转了');
        return
      }


    }, 1000);
  },
  enter(val) {
    clearInterval(this.data.timer)
    const time = setInterval(() => {
      this.setData({
        min: this.data.min - 1,
        timer: time
      })
      console.log(this.data.min);
      if (this.data.min < 1 || val == 'immediately') {
        console.log(time, this.data.timer);
        clearInterval(time)
        wx.navigateTo({
          url: `/pages/filing/inviteViewPlus/inviteViewPlus?familyMemberId=${this.data.familyMemberId}`,
        })
        console.log('跳转了');
        return
      }
    }, 1000);
  },
  goInviteViewPlus() {
    this.getUserInfo('immediately')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, '加载');
    let url = decodeURIComponent(options.q);
    const reg = /familyMemberId=(\d+)/
    const match = url.match(reg)
    const familyMemberId = match ? match[1] : 'err'
    const reg1 = /familyId=(\d+)/
    const match1 = url.match(reg1)
    const familyId = match1 ? match1[1] : 'err'
    SETSTORAGE('inviteFamilyId', familyId)
    console.log(familyMemberId, 'id');
    this.setData({
      familyMemberId: familyMemberId
    })
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log(this.data.timer, '隐藏了');
    clearInterval(this.data.timer)
    this.setData({
      min: 0
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})